package model;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import util.SMSUtil;
import Repository.Repository;
import Repository.RepositoryFactory;
import Repository.UserRepository;
import constants.SMSConstants;

public class PrepareRuleSelector implements RuleSelector {

	private Repository ruleRepository = RepositoryFactory
			.getRepository(SMSConstants.RULE);
	private SMSObject smsObject;

	public PrepareRuleSelector(SMSObject smsObject) {
		this.smsObject = smsObject;
	}

	public boolean processRuleRequest() {
		// System.out.println("Processing Prepare Rule Request....");
		Rule prepareRule = getRule();
		// System.out.println("Got rule.." + prepareRule);
		return applyRule(prepareRule);
	}

	public Rule getRule() {

		// This if is for fetching import rules for lead.
		// We pass an SMS Object and check its type and state to find the rule
		// to retrieve
		if (smsObject instanceof Lead) {
			return (Rule) ruleRepository.retrieve(SMSConstants.LEAD,
					SMSConstants.PREPARE);
		} else // This is for fetching import rules for opportunity
		if (smsObject instanceof Opportunity
				&& smsObject.getCurrentState() instanceof Imported) {
			return (Rule) ruleRepository.retrieve(SMSConstants.OPPORTUNITY,
					SMSConstants.PREPARE);
		} else // This is for fetching import rules for contract
		if (smsObject instanceof Contract
				&& smsObject.getCurrentState() instanceof Imported) {
			return (Rule) ruleRepository.retrieve(SMSConstants.CONTRACT,
					SMSConstants.PREPARE);
		}
		return new NullRule();
	}

	private boolean applyRule(Rule rule) {
		String criteria = rule.getRuleDescripton();
		// System.out.println("Applying Rules...");
		if (smsObject instanceof Lead) {
			Lead objLead = (Lead) smsObject;
			if (smsObject.getCurrentState() instanceof Merged) {
				if (criteria.toLowerCase().equalsIgnoreCase(
						"Customer Interested")) {
					System.out
							.println("Would the lead like to carry forward this deal? (Y/N)");
					String choice = SMSUtil.getUserInput();
					if (choice.equalsIgnoreCase("y")) {
						return true;
					}
				}
			}
		} else if (smsObject instanceof Opportunity) {
			Opportunity objOpp = (Opportunity) smsObject;
			String quantity, price;
			Repository repository = RepositoryFactory
					.getRepository(SMSConstants.OPPORTUNITY);
			if (criteria.toLowerCase().equalsIgnoreCase("non null")) {
				System.out
						.println("Please enter the following details to prepare an opportunity:");
				System.out.println("Quantity:");
				quantity = SMSUtil.getUserInput();
				System.out.println("Price per item:");
				price = SMSUtil.getUserInput();
				objOpp.setQuantity(Integer.parseInt(quantity));
				objOpp.setPrice(Double.parseDouble(price));
				repository.update(objOpp.getSmsObjId(), objOpp);
				return true;
			}
		} else if (smsObject instanceof Contract) {
			Contract objContract = (Contract) smsObject;
			String startDate, endDate, shipAddress, billAddress;
			Repository repository = RepositoryFactory
					.getRepository(SMSConstants.CONTRACT);
			if (criteria.toLowerCase().equalsIgnoreCase("non null")) {
				System.out
						.println("Please enter the following details to prepare a contract:");
				System.out.println("Start date of the contract(mm-dd-yyyy):");
				startDate = SMSUtil.getUserInput();
				System.out.println("End date of the contract(mm-dd-yyyy):");
				endDate = SMSUtil.getUserInput();
				System.out.println("Shipping Address:");
				shipAddress = SMSUtil.getUserInput();
				System.out.println("Biling Address:");
				billAddress = SMSUtil.getUserInput();
				DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
				try {
					objContract.setStartDate(formatter.parse(startDate));
					objContract.setEndDate(formatter.parse(endDate));
					
					//System.out.println("Hlloooo..."+formatter.parse(startDate));

					System.out
							.println("Please Select an approver for the next level :");
					List approverList = objContract.getApprovers();

					List<User> Approvers = UserRepository.getUserList();

					List<User> approversAvail = new ArrayList();

					for (User user : Approvers) {
						if (!approverList.contains(user.getId())) {
							approversAvail.add(user);
						}
					}

					for (User u : approversAvail) {
						System.out.println(u);
					}

					long nextApprover = Long.parseLong(SMSUtil.getUserInput());
					objContract.setNextApprover(nextApprover);
					objContract.setShippingAddress(shipAddress);
					objContract.setBillingAddress(billAddress);
					ContractRule cr = (ContractRule) rule;
					objContract.setMaxLevel(cr.getMaxLevel(objContract));
					repository.update(objContract.getSmsObjId(), objContract);
					return true;
				} catch (ParseException pe) {
					System.out.println("Incorrect date format!!");
				}
			}
		}
		return false;
	}
}
